package com.example.springboot_ruiji.controller;

import com.example.springboot_ruiji.common.R;
import com.example.springboot_ruiji.pojo.DishDTO;
import com.example.springboot_ruiji.pojo.SetMealDto;
import com.example.springboot_ruiji.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    @GetMapping("page")
    public R page(String name, @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer pageSize){
        /*String name1="%"+name+"%";*/
        return setMealService.page(name,page,pageSize);
    }

    @PostMapping
    public R addSetMeal(@RequestBody SetMealDto setMealDto , HttpSession session){
        //1. 补全创建人与修改人
        Long empId = (Long) session.getAttribute("employee");
        setMealDto.setCreateUser(empId);
        setMealDto.setUpdateUser(empId);
        //2.把数据交给service
        return setMealService.save(setMealDto);
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        SetMealDto setMealDto = setMealService.findById(id);
        return R.success(setMealDto);
    }

    @PutMapping
    public R updateDish(@RequestBody  SetMealDto setMealDto,HttpSession session){
        //1. 补全创建人与修改人
        Long empId = (Long) session.getAttribute("employee");
        setMealDto.setUpdateUser(empId);
        //2. 交给service
        setMealService.update(setMealDto);
        return R.success("修改菜品成功");
    }

    @DeleteMapping
    public R batchDelete(String ids){
        return setMealService.batchDelete(ids);
    }

    @PostMapping("status/{status}")
    public R dishStatusByStatus(String ids,@PathVariable Integer status,HttpSession session){
        log.info("s:{}",status);
        Long id = (Long) session.getAttribute("employee");
        SetMealDto setMealDto = new SetMealDto();
        setMealDto.setUpdateUser(id);
        return setMealService.dishStatusByStatus(setMealDto,ids,status);
    }
}
